﻿$(function () {
    UseDialog();
    UseForm();
})


function GetUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var Url = window.location.href;

    if (Url.indexOf("?") > 0)
        Url = "&" + Url.substr(Url.indexOf("?") + 1);

    var r = Url.match(reg); //匹配目标参数
    if (r != null) return unescape(r[2]);
    return ''; //返回参数值
}

function GetHeadInfo() {
    var headinfo = {
        "Content-Type": "application/json;charset=uft-8"
    };
    return headinfo;
}

function HttpPost(JsonData, Url, SucessFunctionName) {
    if (Url.indexOf("?") < 0)
        Url = Url + "?t=" + Math.random();
    else
        Url = Url + "&t=" + Math.random();

    $.ajax({
        url: GetRootPath() + Url,
        type: "POST",
        headers: GetHeadInfo(),
        data: JsonData,
        dataType: 'json',
        complete: function () { },
        success: SucessFunctionName,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //var strLog = "详细错误：" + XMLHttpRequest.responseText;                //writeFile("c:\\qd.log", strLog);
            ///alert(errorThrown + ";详细错误：" + XMLHttpRequest.responseText);
        }
    });
}

function HttpPost_Sync(JsonData, Url, SucessFunctionName) {
    if (Url.indexOf("?") < 0)
        Url = Url + "?t=" + Math.random();
    else
        Url = Url + "&t=" + Math.random();

    $.ajax({
        async :false,
        url: GetRootPath() + Url,
        type: "POST",
        headers: GetHeadInfo(),
        data: JsonData,
        dataType: 'json',
        complete: function () { },
        success: SucessFunctionName,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //var strLog = "详细错误：" + XMLHttpRequest.responseText;                //writeFile("c:\\qd.log", strLog);
            ///alert(errorThrown + ";详细错误：" + XMLHttpRequest.responseText);
        }
    });
}

function HttpForm(FormData, Url, SucessFunctionName) {
    if (Url.indexOf("?") < 0)
        Url = Url + "?t=" + Math.random();
    else
        Url = Url + "&t=" + Math.random();

    $.ajax({
        url: GetRootPath() + Url,
        type: "POST",
        headers: {},
        data: FormData,
        dataType: 'json',
        cache: false, // 不缓存
        processData: false, // jQuery不要去处理发送的数据
        contentType: false, // jQuery不要去设置
        complete: function () { },
        success: SucessFunctionName,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //var strLog = "详细错误：" + XMLHttpRequest.responseText;                //writeFile("c:\\qd.log", strLog);
            ///alert(errorThrown + ";详细错误：" + XMLHttpRequest.responseText);
        }
    });
}

function getNewGuid() {
    var date = new Date();
    var dateGuid = convertDateToString(date);
    var randomGuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = Math.random() * 16 | 0,
            v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    })
    return dateGuid + randomGuid;
}

function convertDateToString(now) {
    var year = now.getFullYear();
    var month = (now.getMonth() + 1).toString();
    var day = (now.getDate()).toString();
    var hour = (now.getHours()).toString();
    var minute = (now.getMinutes()).toString();
    var second = (now.getSeconds()).toString();
    if (month.length == 1) {
        month = "0" + month;
    }
    if (day.length == 1) {
        day = "0" + day;
    }
    if (hour.length == 1) {
        hour = "0" + hour;
    }
    if (minute.length == 1) {
        minute = "0" + minute;
    }
    if (second.length == 1) {
        second = "0" + second;
    }
    var dateTime = year + month + day + hour + minute + second;
    return dateTime;
}

function sleep(delay) {
    var start = (new Date()).getTime();
    while ((new Date()).getTime() - start < delay) {
        continue;
    }
}


